import java.util.Arrays;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86136
 * Date: 2024-07-04
 * Time: 16:05
 */
class Person implements Cloneable{
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
public class Test {
    public static void fuc22() {
        int[] arr = {1,2,3};
        System.out.println(arr[20]);
    }
    public static void main(String[] args) {
        fuc22();
        System.out.println("try-catch后语句");
    }
    public static void fuc2() {
        int[] arr = {1,2,3};
        System.out.println(arr[20]);
    }
    public static void main11(String[] args) {
        try {
            fuc2();
            System.out.println("try中最后一条语句");
        }catch (ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();
        }
        System.out.println("try-catch后语句");
    }
    public static int testFuc() {
        try {
            return 10;
        }finally {
            return 20;
        }
    }
    public static void main0(String[] args) {
        System.out.println(testFuc());
    }
    public static int getData() {
        Scanner scanner = null;
        try {
            scanner = new Scanner(System.in);
            int data = scanner.nextInt();
            return data;
        }catch (NullPointerException e) {
            e.printStackTrace();
        }finally {
            System.out.println("finally中代码");
        }
        if(scanner != null) {
            scanner.close();
        }
        return 0;
    }
    public static void main9(String[] args) {
        int data = getData();
        System.out.println(data);
    }
    public static void main8(String[] args) {
        int[] array = {1,2,3};
        try {
            array = null;
            System.out.println(array.length);
            System.out.println(array[10]);
        }catch (Exception e) {
            System.out.println("处理Exception异常");
        }finally {
            System.out.println("无论是否有异常都会执行");
        }
        System.out.println("继续执行");
    }
    public static void main7(String[] args) {
        int[] array = {1,2,3};
        try {
            array = null;
            System.out.println(array.length);
        }catch (ArithmeticException e) {
            System.out.println("处理ArrayIndexOutOfBoundsException异常");
        }finally {
            System.out.println("无论是否有异常都会执行");
        }
        System.out.println("继续执行");
    }
    public static void fuc(int[] array,int index) {
        if(array == null) {
            throw new NullPointerException("数组为null");
        }
        if (index < 0 || index >= array.length) {
            throw new ArrayIndexOutOfBoundsException("数组下标越界");
        }
    }
    public static void main6(String[] args) {
        int[] array = {1,2,3};
        fuc(array,3);
    }
    public static void main5(String[] args) {
        int[] array = {1,2,3};
        try {
            System.out.println(array[10]);
            array = null;
            System.out.println(array.length);
        }catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("处理ArrayIndexOutOfBoundsException异常");
            e.printStackTrace();//将错误信息全部打印至控制台上展示
        }catch (NullPointerException e) {
            System.out.println("处理NullPointerException异常");
            e.printStackTrace();//将错误信息全部打印至控制台上展示
        }
    }
    public static void function(int a) throws ArithmeticException {
        if(a == 0) {
            throw new ArithmeticException("a == 0");
        }
    }
    public static void main4(String[] args) {
        try {
            function(0);
            System.out.println("下一条");
        }catch (ArithmeticException e) {
            System.out.println("捕获了异常，处理异常");
        }
        System.out.println("程序继续正常执行");
    }
    public static void testException(int a) {
        if(a == 0) {
            throw new ArithmeticException("a == 0");
            //System.out.println("aadafv");//异常后面不能再加
        }
    }
    public static void main3(String[] args) {
        testException(0);
    }
    public static void main2(String[] args) throws CloneNotSupportedException{
        //System.out.println(10/0);//ArithmeticException
        //int[] array = new int[]{1,2,3,4,5};
        //System.out.println(array[10]);//ArrayIndexOutOfBoundsException
        int[] array = null;
        //System.out.println(array[0]);//NullPointerException
        System.out.println(array.length);//NullPointerException
        Person person = new Person();
        Person person1 = (Person) person.clone();//必须对其进行捕获或声明以便抛出

    }
    public int countSegments(String s) {
        if(s.isEmpty()) {
            return 0;
        }
        String[] ch = s.split(" ");
        int count = 0;
        for(String ss : ch) {
            if(ss.length() != 0) {
                count++;
            }
        }
        return count;
    }
    public String toLowerCase(String s) {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if(Character.isLetter(ch)) {
                if(Character.isUpperCase(ch)) {
                    ch = (char)(ch + 32);
                }
            }
            stringBuilder.append(ch);
        }
        return stringBuilder.toString();
    }
    public static void main1(String[] args) {
        Test test = new Test();
        String s = test.toLowerCase("HELLO");
        int i = test.countSegments(", , , ,        a, eaefa");
        System.out.println(s + " " + i);
    }
}
